home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / zdpnext.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  12.3 KB  |  439 lines

  1. /* Copyright (C) 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zdpnext.c */
  20. /* NeXT Display PostScript extensions */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "gscspace.h"        /* for iimage.h */
  25. #include "gsmatrix.h"
  26. #include "gsiparam.h"        /* for iimage.h */
  27. #include "gxcvalue.h"
  28. #include "gxsample.h"
  29. #include "ialloc.h"
  30. #include "igstate.h"
  31. #include "iimage.h"
  32.  
  33. /*
  34.  * Miscellaneous notes:
  35.  *
  36.  * composite / dissolve respect destination clipping (both clip & viewclip),
  37.  *   but ignore source clipping.
  38.  * composite / dissolve must handle overlapping source/destination correctly.
  39.  * compositing converts the source to the destination's color model
  40.  *   (including halftoning if needed).
  41.  */
  42.  
  43. /*
  44.  * Define the compositing operations.  Eventually this will be moved to
  45.  * a header file in the library.  These values must match the ones in
  46.  * dpsNeXT.h.
  47.  */
  48. typedef enum {
  49.   NX_CLEAR = 0,
  50.   NX_COPY,
  51.   NX_SOVER,
  52.   NX_SIN,
  53.   NX_SOUT,
  54.   NX_SATOP,
  55.   NX_DOVER,
  56.   NX_DIN,
  57.   NX_DOUT,
  58.   NX_DATOP,
  59.   NX_XOR,
  60.   NX_PLUSD,
  61.   NX_HIGHLIGHT,            /* only for compositerect */
  62.   NX_PLUSL,
  63. #define NX_composite_last NX_PLUSL
  64. #define NX_compositerect_last NX_PLUSL
  65.   NX_DISSOLVE            /* fake value used internally for dissolve */
  66. } gs_composite_op_t;
  67.  
  68. /*
  69.  * Define the parameters for a compositing operation at the library level.
  70.  * Of course this too belongs in the library.
  71.  */
  72. typedef struct gs_composite_params_s {
  73.   gs_composite_op_t cop;
  74.   gx_color_value delta;        /* only for dissolve */
  75.   byte src_value[5*2];        /* only if src == 0 or !src_has_alpha */
  76. } gs_composite_params_t;
  77.  
  78. /* Define a local structure for holding compositing operands. */
  79. typedef struct gs_composite_operands_s {
  80.   float src_rect[4];        /* x, y, width, height */
  81.   gs_state *src_gs;
  82.   float dest_pt[2];        /* x, y */
  83. } gs_composite_operands_t;
  84.  
  85. /* Imported procedures */
  86. int zimage_multiple(P2(os_ptr op, bool has_alpha));  /* in zcolor1.c */
  87.  
  88. /* Forward references */
  89. private int composite_operands(P2(gs_composite_operands_t *, os_ptr));
  90. private int rect_param(P2(os_ptr, float [4]));
  91.  
  92. /* <width> <height> <bits/comp> <matrix> */
  93. /*    <datasrc_0> ... <datasrc_ncomp-1> true <ncomp> alphaimage - */
  94. /*    <datasrc> false <ncomp> alphaimage - */
  95. private int
  96. zalphaimage(register os_ptr op)
  97. {    /* Essentially the whole implementation is shared with colorimage. */
  98.     return zimage_multiple(op, true);
  99. }
  100.  
  101. /* <srcx> <srcy> <width> <height> <srcgstate|null> <destx> <desty> <op> */
  102. /*   composite - */
  103. private int
  104. zcomposite(register os_ptr op)
  105. {    gs_composite_operands_t operands;
  106.     int code = composite_operands(&operands, op);
  107.     gs_composite_params_t params;
  108.  
  109.     if ( code < 0 )
  110.       return code;
  111.     check_int_leu(*op, NX_composite_last);
  112.     params.cop = (gs_composite_op_t)op->value.intval;
  113.     if ( params.cop == NX_HIGHLIGHT )
  114.       return_error(e_rangecheck);
  115.     /****** NYI ******/
  116.     return_error(e_undefined);
  117. }
  118.  
  119. /* <destx> <desty> <width> <height> <op> compositerect - */
  120. private int
  121. zcompositerect(register os_ptr op)
  122. {    float dest_rect[4];
  123.     gs_composite_params_t params;
  124.     int code = rect_param(op - 1, dest_rect);
  125.  
  126.     if ( code < 0 )
  127.       return code;
  128.     check_int_leu(*op, NX_compositerect_last);
  129.     params.cop = (gs_composite_op_t)op->value.intval;
  130.     /****** SET src_value ******/
  131.     /****** NYI ******/
  132.     return_error(e_undefined);
  133. }
  134.  
  135. /* <srcx> <srcy> <width> <height> <srcgstate|null> <destx> <desty> <delta> */
  136. /*   dissolve - */
  137. private int
  138. zdissolve(register os_ptr op)
  139. {    gs_composite_operands_t operands;
  140.     int code = composite_operands(&operands, op);
  141.     gs_composite_params_t params;
  142.     float delta;
  143.  
  144.     if ( code < 0 )
  145.       return code;
  146.     code = num_params(op, 1, &delta);
  147.     if ( code < 0 )
  148.       return code;
  149.     if ( delta < 0 || delta > 1 )
  150.       return_error(e_rangecheck);
  151.     params.cop = NX_DISSOLVE;
  152.     params.delta = (gx_color_value)(delta * gx_max_color_value);
  153.     /****** NYI ******/
  154.     return_error(e_undefined);
  155. }
  156.  
  157. /* ------ Initialization procedure ------ */
  158.  
  159. BEGIN_OP_DEFS(zdpnext_op_defs) {
  160.     {"7alphaimage", zalphaimage},
  161.     {"8composite", zcomposite},
  162.     {"5compositerect", zcompositerect},
  163.     {"8dissolve", zdissolve},
  164. END_OP_DEFS(0) }
  165.  
  166. /* ------ Internal routines ------ */
  167.  
  168. /* Collect a rect operand. */
  169. private int
  170. rect_param(os_ptr op, float rect[4])
  171. {    int code = num_params(op, 4, rect);
  172.  
  173.     if ( code < 0 )
  174.       return code;
  175.     if ( rect[2] < 0 )
  176.       rect[0] += rect[2], rect[2] = -rect[2];
  177.     if ( rect[3] < 0 )
  178.       rect[1] += rect[3], rect[3] = -rect[3];
  179.     return code;
  180. }
  181.  
  182. /* Collect parameters for a compositing operation. */
  183. private int
  184. composite_operands(gs_composite_operands_t *pco, os_ptr op)
  185. {    int code = rect_param(op - 4, pco->src_rect);
  186.  
  187.     if ( code < 0 ||
  188.          (code = num_params(op - 1, 2, pco->dest_pt)) < 0
  189.        )
  190.       return code;
  191.     if ( r_has_type(op - 3, t_null) )
  192.       pco->src_gs = igs;
  193.     else {
  194.       check_stype(op[-3], st_igstate_obj);
  195.       check_read(op[-3]);
  196.       pco->src_gs = igstate_ptr(op - 3);
  197.     }
  198.     return 0;
  199. }
  200.  
  201. /* ------ Library procedures ------ */
  202.  
  203. /*
  204.  * Composite two arrays of (premultiplied) pixel values.
  205.  * Legal values of bits_per_value are 1, 2, 4, 8, and 16.
  206.  * src_has_alpha indicates whether or not the source has alpha values;
  207.  * if not, the alpha value is constant (in the parameter structure).
  208.  * Legal values of values_per_pixel are 1-5, including alpha.
  209.  * (The source has one fewer value per pixel if it doesn't have alpha.)
  210.  * src_data = 0 is legal and indicates that the source value is constant,
  211.  * specified in the parameter structure.
  212.  *
  213.  * The current implementation is simple but inefficient.  We'll speed it up
  214.  * later if necessary.
  215.  */
  216. private int
  217. composite_values(byte *dest_data, int dest_x, const byte *src_data, int src_x,
  218.   int bits_per_value, int values_per_pixel, bool src_has_alpha,
  219.   uint num_pixels, const gs_composite_params_t *pcp, gs_memory_t *mem)
  220. {    int src_vpp = values_per_pixel - (src_has_alpha ? 0 : 1);
  221.     int bytes_per_value = bits_per_value >> 3;
  222.     int dest_bpp, src_bpp;
  223.     uint highlight_value = (1 << bits_per_value) - 1;
  224.     byte *dbuf = 0;
  225.     byte *sbuf = 0;
  226.     int dx;
  227.     byte *dptr;
  228.     const byte *sptr;
  229.       
  230.     if ( bytes_per_value == 0 ) {
  231.       /*
  232.        * Unpack the operands now, and repack the results after the
  233.        * operation.
  234.        */
  235.       sample_unpack_proc((*unpack_proc));
  236.       static const bits16 lookup2x2[16] = {
  237. #if arch_is_big_endian
  238. #  define map2(a,b) (a * 0x5500 + b * 0x0055)
  239. #else
  240. #  define map2(a,b) (a * 0x0055 + b * 0x5500)
  241. #endif
  242. #define map4x2(a) map2(a,0), map2(a,1), map2(a,2), map2(a,3)
  243.         map4x2(0), map4x2(1), map4x2(2), map4x2(3)
  244. #undef map2
  245. #undef map4x2
  246.       };
  247.       static const byte lookup4[16] = {
  248.         0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  249.         0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  250.       };
  251.       const sample_lookup_t *lookup;
  252.       int sx;
  253.  
  254.       switch ( bits_per_value ) {
  255.       case 1:
  256.         unpack_proc = sample_unpack_1;
  257.         lookup = sample_lookup_1_identity;
  258.         break;
  259.       case 2:
  260.         unpack_proc = sample_unpack_2;
  261.         lookup = (const sample_lookup_t *)lookup2x2;
  262.         break;
  263.       case 4:
  264.         unpack_proc = sample_unpack_4;
  265.         lookup = (const sample_lookup_t *)lookup4;
  266.         break;
  267.       default:
  268.         return_error(e_rangecheck);
  269.       }
  270.       /*
  271.        * Because of partial input bytes due to dest_/src_x,
  272.        * we may need as many as 3 extra pixels in the buffers.
  273.        */
  274.       dbuf = gs_alloc_bytes(mem, (num_pixels + 3) * values_per_pixel,
  275.                 "composite_values(dbuf)");
  276.       if ( src_data )
  277.         sbuf = gs_alloc_bytes(mem, (num_pixels + 3) * src_vpp,
  278.                   "composite_values(sbuf)");
  279.       if ( (sbuf == 0 && src_data) || dbuf == 0 ) {
  280.         gs_free_object(mem, sbuf, "composite_values(sbuf)");
  281.         gs_free_object(mem, dbuf, "composite_values(dbuf)");
  282.         return_error(e_VMerror);
  283.       }
  284.       dptr = (*unpack_proc)(dbuf, &dx, dest_data, dest_x,
  285.                 ((dest_x + num_pixels) * bits_per_value *
  286.                  values_per_pixel + 7) >> 3,
  287.                 lookup, 1);
  288.       if ( src_data )
  289.         sptr = (*unpack_proc)(sbuf, &sx, src_data, src_x,
  290.                   ((src_x + num_pixels) * bits_per_value *
  291.                    src_vpp + 7) >> 3,
  292.                   lookup, 1);
  293.       src_vpp = bytes_per_value = 1;
  294.       dest_bpp = values_per_pixel;
  295.       src_bpp = src_vpp;
  296.       dptr += dx * dest_bpp;
  297.       sptr += sx * src_bpp;
  298.     } else {
  299.       dest_bpp = bytes_per_value * values_per_pixel;
  300.       src_bpp = bytes_per_value * src_vpp;
  301.       dptr = dest_data + dest_x * dest_bpp;
  302.       sptr = src_data + src_x * src_bpp;
  303.     }
  304.     {
  305. #define bits16_get(p) ( ((p)[0] << 8) + (p)[1] )
  306. #define bits16_put(p,v) ( (p)[0] = (byte)((v) >> 8), (p)[1] = (byte)(v) )
  307.       uint max_value = (1 << bits_per_value) - 1;
  308.       uint delta_v = pcp->delta * max_value / gx_max_color_value;
  309.       int src_no_alpha_j;
  310.       uint src_alpha;
  311.       uint x;
  312.  
  313.       if ( src_has_alpha )
  314.         src_no_alpha_j = -1;
  315.       else {
  316.         src_alpha =
  317.           (bytes_per_value == 1 ?
  318.            pcp->src_value[values_per_pixel - 1] :
  319.            bits16_get(&pcp->src_value[src_bpp - 2]));
  320.         src_no_alpha_j = 0;
  321.       }
  322.       for ( x = 0; x < num_pixels; ++x ) {
  323.         uint dest_alpha;
  324.         int j;
  325.  
  326.         if ( !src_data )
  327.           sptr = pcp->src_value;
  328.         if ( bytes_per_value == 1 ) {
  329.           dest_alpha = dptr[values_per_pixel - 1];
  330.           if ( src_has_alpha )
  331.         src_alpha = sptr[values_per_pixel - 1];
  332.         } else {
  333.           dest_alpha =
  334.         bits16_get(&dptr[dest_bpp - 2]);
  335.           if ( src_has_alpha )
  336.         src_alpha =
  337.           bits16_get(&sptr[src_bpp - 2]);
  338.         }
  339. #define fr(v, a) ((v) * (a) / max_value)
  340. #define nfr(v, a) ((v) * (max_value - (a)) / max_value)
  341.         for ( j = values_per_pixel; --j != 0; ) {
  342.           uint dest_v, src_v, result;
  343. #define set_clamped(r, v) if ( (r = (v)) > max_value ) r = max_value
  344.  
  345.           if ( bytes_per_value == 1 ) {
  346.         dest_v = *dptr;
  347.         src_v = (j == src_no_alpha_j ? src_alpha : *sptr++);
  348.           } else {
  349.         dest_v = bits16_get(dptr);
  350.         if ( j == src_no_alpha_j )
  351.           src_v = src_alpha;
  352.         else
  353.           src_v = bits16_get(sptr), sptr += 2;
  354.           }
  355.           switch ( pcp->cop ) {
  356.           case NX_CLEAR:
  357.         result = 0;
  358.         break;
  359.           case NX_COPY:
  360.         result = src_v;
  361.         break;
  362.           case NX_PLUSD:
  363.         /*
  364.          * This is the only case where we have to worry about
  365.          * clamping a possibly negative result.
  366.          */
  367.         result = src_v + dest_v;
  368.         result = (result < max_value ? 0 : result - max_value);
  369.         break;
  370.           case NX_PLUSL:
  371.         set_clamped(result, src_v + dest_v);
  372.         break;
  373.           case NX_SOVER:
  374.         set_clamped(result, src_v + nfr(dest_v, src_alpha));
  375.         break;
  376.           case NX_DOVER:
  377.         set_clamped(result, nfr(src_v, dest_alpha) + dest_v);
  378.         break;
  379.           case NX_SIN:
  380.         result = fr(src_v, dest_alpha);
  381.         break;
  382.           case NX_DIN:
  383.         result = fr(dest_v, src_alpha);
  384.         break;
  385.           case NX_SOUT:
  386.         result = nfr(src_v, dest_alpha);
  387.         break;
  388.           case NX_DOUT:
  389.         result = nfr(dest_v, src_alpha);
  390.         break;
  391.           case NX_SATOP:
  392.         set_clamped(result, fr(src_v, dest_alpha) +
  393.                 nfr(dest_v, src_alpha));
  394.         break;
  395.           case NX_DATOP:
  396.         set_clamped(result, nfr(src_v, dest_alpha) +
  397.                 fr(dest_v, src_alpha));
  398.         break;
  399.           case NX_XOR:
  400.         set_clamped(result, nfr(src_v, dest_alpha) +
  401.                 nfr(dest_v, src_alpha));
  402.         break;
  403.           case NX_HIGHLIGHT:
  404.         /*
  405.          * Bizarre but true: this operation converts white and
  406.          * light gray into each other, and leaves all other values
  407.          * unchanged.  We only implement it properly for gray-scale
  408.          * devices.
  409.          */
  410.         if ( j != 0 && !((src_v ^ highlight_value) & ~1) )
  411.           result = src_v ^ 1;
  412.         else
  413.           result = src_v;
  414.         break;
  415.           case NX_DISSOLVE:
  416.         result = fr(src_v, delta_v) + nfr(dest_v, delta_v);
  417.         break;
  418.           default:
  419.         return_error(e_rangecheck);
  420.           }
  421.           if ( bytes_per_value == 1 )
  422.         *dptr++ = (byte)result;
  423.           else
  424.         bits16_put(dptr, result), dptr += 2;
  425.         }
  426.       }
  427.     }
  428.     if ( dbuf ) {
  429.       /*
  430.        * Pack pixels back into the destination.  This is modeled on
  431.        * the line_accum macros in gxcindex.h.
  432.        */
  433.       /****** TBC ******/
  434.       gs_free_object(mem, sbuf, "composite_values(sbuf)");
  435.       gs_free_object(mem, dbuf, "composite_values(dbuf)");
  436.     }
  437.     return 0;
  438. }
  439.